package com.layui.ad.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.layui.ad.config.SystemConfig;
import com.layui.ad.entity.SysAdmin;
import com.layui.ad.service.BrokerService;
import com.layui.ad.service.SysAdminService;
import com.layui.ad.utils.JsonResult;
import com.layui.ad.utils.ShiroUtils;
import com.layui.ad.vo.MenuListVo;
import lombok.extern.log4j.Log4j2;
import com.layui.ad.service.SysMenuService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import com.layui.ad.utils.RedisUtil;


@Controller
@RequestMapping("")
@Log4j2
public class IndexController extends BaseController{
    @Autowired
    HttpServletRequest request;
    @Autowired
    public BrokerService brokerService;

    @Autowired
    public SysAdminService gmSysAdminService;

    @Autowired
    public SysMenuService sysMenuService;

    @Autowired
    public RedisUtil redisUtil;


    @RequestMapping("")
    public String index(HttpServletRequest request){
//        Page<BrokerInfo> page = new Page<>(11, 30);
//        BrokerInfo binfo = new BrokerInfo();
//        binfo.setCompanysid(22);
//        IPage<BrokerInfo> brokerInfoIPage = brokerService.selectRecord(page,binfo);
//        System.out.println(brokerInfoIPage);
//        request.setAttribute("record",brokerInfoIPage);
//        List<SysAdmin> plist = gmSysAdminService.selectRecord();
        List<MenuListVo> menulist = sysMenuService.getNavbarMenu();
        SysAdmin user = gmSysAdminService.getUserInfo(ShiroUtils.getAdminId());
        request.setAttribute("nickName", SystemConfig.nickName);
        request.setAttribute("version", SystemConfig.version);
        request.setAttribute("menuList", menulist);
        request.setAttribute("user",user);
        redisUtil.set("name","liangpingzheng");
        Object name = redisUtil.get("name");
        System.out.print(name);
        return "index/default";
    }

    @RequestMapping("/main")
    public String main(){
        return "index/main";
    }

    public <T> Page<T> getPage() {
        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isBlank(pageNoStr)) {
            pageNoStr = request.getParameter("pageNumber");
        }

        if (StringUtils.isBlank(pageNoStr)) {
            pageNoStr = request.getParameter("current");
        }
        //
        String pageSizeStr = request.getParameter("pageSize");
        if (StringUtils.isBlank(pageSizeStr)) {
            pageSizeStr = request.getParameter("size");
        }
        //
        long pageNo = 0;
        long pageSize = 10;
        if (NumberUtils.isDigits(pageNoStr)) {
            pageNo = Integer.parseInt(pageNoStr);
        }
        if (NumberUtils.isDigits(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        if (pageSize > 100) {
            pageSize = 100;
        }
        return new Page<T>(pageNo, pageSize);
    }

    @GetMapping("userInfo")
    public String userInfo(HttpServletRequest request){
        Integer adminId = ShiroUtils.getAdminId();
        Map<String,Object> info = gmSysAdminService.info(adminId);
        request.setAttribute("info",info);
        return "userinfo";
    }

    @ResponseBody
    @PostMapping("/updateUserInfo")
    public JsonResult updateUserInfo(@RequestBody SysAdmin entiry){
//        SysAdmin admin = new SysAdmin();
//        admin.setId(entiry.getId());
        entiry.setId(ShiroUtils.getAdminId());
        boolean flag = gmSysAdminService.updateById(entiry);
        if(flag) {
            return JsonResult.ok();
        }else{
            return JsonResult.fail();
        }
    }
}
